DCGAN Generator & Discriminator

합성곱 GAN
생성자와 판별자의 특성 맵은 다른 특성을 가진다.
생성자에서는 많은 특성 맵 개수에서 시작해서 마지막 층으로 갈 수록 줄어든다.
판별자는 작은 채널 개수로 시작해서 마지막 층으로 갈수록 늘어난다.
(특성맵의 개수와 특성 맵의 공간 차원의 크기가 반대, 특성 맵의 공간 크기가 증가하면, 특성 맵의 개수가 줄어든다.)

배치 정규화 앞의 층에서는 절편 유닛을 사용하지 않는 것이 보통 권장됨
Generator & Discriminator (DCGAN)
import tensorflow as tf
import numpy as np
def make_dcgan_generator(z_size=20, output_size=(28, 28, 1), n_filters=128, n_blocks=2):
size_factor=2**n_blocks
hidden_size=(output_size[0]//size_factor, output_size[1]//size_factor)
model=tf.keras.Sequential([
tf.keras.layers.Input(shape=(z_size, )),
tf.keras.layers.Dense(units=n_filters*np.prod(hidden_size), use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU(),
tf.keras.layers.Reshape((hidden_size[0], hidden_size[1], n_filters)),
tf.keras.layers.Conv2DTranspose(filters=n_filters, kernel_size=(5, 5), strides=(1, 1), padding='same', use_bias=False),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU()])
nf=n_filters
for i in range(n_blocks):
nf=nf//2
model.add(tf.keras.layers.Conv2DTranspose(
filters=nf, kernel_size=(5, 5), strides=(2, 2), padding='same', use_bias=False))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.LeakyReLU())
model.add(tf.keras.layers.Conv2DTranspose(
filters=output_size[2], kernel_size=(5, 5), strides=(1, 1), padding='same', use_bias=False, activation='tanh'))
return model
def make_dcgan_discriminator(input_size=(28, 28, 1), n_filters=64, n_blocks=2):
model=tf.keras.Sequential([
tf.keras.layers.Input(shape=input_size),
tf.keras.layers.Conv2D(filters=n_filters, kernel_size=5, strides=(1, 1), padding='same'),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.LeakyReLU()])
nf=n_filters
for i in range(n_blocks):
nf=nf*2
model.add(tf.keras.layers.Conv2D(
filters=nf, kernel_size=(5, 5), strides=(2, 2), padding='same'))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.LeakyReLU())
model.add(tf.keras.layers.Dropout(0.3))
model.add(tf.keras.layers.Conv2D(filters=1, kernel_size=(7, 7), padding='valid'))
model.add(tf.keras.layers.Reshape((1, )))
return model
mnist data & build
GAN 모델에서 preprocess는 데이터를 펼친 벡터 형태로 반환했지만, DCGAN에서는 이미지 텐서를 출력
import tensorflow_datasets as tfds
mnist_bldr=tfds.builder('mnist')
mnist_bldr.download_and_prepare()
mnist=mnist_bldr.as_dataset(shuffle_files=False)
def preprocess(ex, mode='uniform'):
image=ex['image']
image=tf.image.convert_image_dtype(image, tf.float32)
image=image*2-1.0
if mode=='uniform':
input_z=tf.random.uniform(shape=(z_size, ), minval=-1.0, maxval=1.0)
elif mode=='normal':
input_z=tf.random.normal(shape=(z_size, ))
return input_z, image
gen_model=make_dcgan_generator()
disc_model=make_dcgan_discriminator()
gen_model.summary()
disc_model.summary()

Model: "sequential"

_________________________________________________________________

Layer (type)                 Output Shape              Param #   

=================================================================

dense (Dense)                (None, 6272)              125440    

_________________________________________________________________

batch_normalization (BatchNo (None, 6272)              25088     

_________________________________________________________________

leaky_re_lu (LeakyReLU)      (None, 6272)              0         

_________________________________________________________________

reshape (Reshape)            (None, 7, 7, 128)         0         

_________________________________________________________________

conv2d_transpose (Conv2DTran (None, 7, 7, 128)         409600    

_________________________________________________________________

batch_normalization_1 (Batch (None, 7, 7, 128)         512       

_________________________________________________________________

leaky_re_lu_1 (LeakyReLU)    (None, 7, 7, 128)         0         

_________________________________________________________________

conv2d_transpose_1 (Conv2DTr (None, 14, 14, 64)        204800    

_________________________________________________________________

batch_normalization_2 (Batch (None, 14, 14, 64)        256       

_________________________________________________________________

leaky_re_lu_2 (LeakyReLU)    (None, 14, 14, 64)        0         

_________________________________________________________________

conv2d_transpose_2 (Conv2DTr (None, 28, 28, 32)        51200     

_________________________________________________________________

batch_normalization_3 (Batch (None, 28, 28, 32)        128       

_________________________________________________________________

leaky_re_lu_3 (LeakyReLU)    (None, 28, 28, 32)        0         

_________________________________________________________________

conv2d_transpose_3 (Conv2DTr (None, 28, 28, 1)         800       

=================================================================

Total params: 817,824

Trainable params: 804,832

Non-trainable params: 12,992

_________________________________________________________________



Model: "sequential_1"

_________________________________________________________________

Layer (type)                 Output Shape              Param #   

=================================================================

conv2d (Conv2D)              (None, 28, 28, 64)        1664      

_________________________________________________________________

batch_normalization_4 (Batch (None, 28, 28, 64)        256       

_________________________________________________________________

leaky_re_lu_4 (LeakyReLU)    (None, 28, 28, 64)        0         

_________________________________________________________________

conv2d_1 (Conv2D)            (None, 14, 14, 128)       204928    

_________________________________________________________________

batch_normalization_5 (Batch (None, 14, 14, 128)       512       

_________________________________________________________________

leaky_re_lu_5 (LeakyReLU)    (None, 14, 14, 128)       0         

_________________________________________________________________

dropout (Dropout)            (None, 14, 14, 128)       0         

_________________________________________________________________

conv2d_2 (Conv2D)            (None, 7, 7, 256)         819456    

_________________________________________________________________

batch_normalization_6 (Batch (None, 7, 7, 256)         1024      

_________________________________________________________________

leaky_re_lu_6 (LeakyReLU)    (None, 7, 7, 256)         0         

_________________________________________________________________

dropout_1 (Dropout)          (None, 7, 7, 256)         0         

_________________________________________________________________

conv2d_3 (Conv2D)            (None, 1, 1, 1)           12545     

_________________________________________________________________

reshape_1 (Reshape)          (None, 1)                 0         

=================================================================

Total params: 1,040,385

Trainable params: 1,039,489

Non-trainable params: 896

_________________________________________________________________